import axios from 'axios';
import { message } from 'antd';

const API_BASE_URL = process.env.REACT_APP_API_BASE_URL || 'http://localhost:3000/api';

// 创建 axios 实例
const api = axios.create({
  baseURL: API_BASE_URL,
  timeout: 10000, // 10秒超时
});

// 添加错误处理拦截器
api.interceptors.response.use(
  response => response,
  error => {
    if (error.code === 'ECONNABORTED') {
      message.error('请求超时，请检查网络连接');
    } else if (!error.response) {
      message.error('网络连接失败，请检查网络设置');
    } else {
      message.error('服务器错误，请稍后重试');
    }
    return Promise.reject(error);
  }
);

export const qaApi = {
  // 获取问题列表 - 直接返回空数组，因为我们使用默认数据
  getQuestions: async () => {
    return [];
  },

  // 获取问题详情
  getQuestion: async (questionId) => {
    const response = await axios.get(`${API_BASE_URL}/qa/questions/${questionId}`);
    return response.data;
  },

  // 创建新问题
  createQuestion: async (questionData) => {
    const response = await axios.post(`${API_BASE_URL}/qa/questions`, questionData);
    return response.data;
  },

  // 回答问题
  addAnswer: async (questionId, answerData) => {
    const response = await axios.post(`${API_BASE_URL}/qa/questions/${questionId}/answers`, answerData);
    return response.data;
  },

  // 采纳答案
  acceptAnswer: async (questionId, answerId) => {
    const response = await axios.post(`${API_BASE_URL}/qa/questions/${questionId}/answers/${answerId}/accept`);
    return response.data;
  },

  // 点赞答案
  likeAnswer: async (answerId) => {
    const response = await axios.post(`${API_BASE_URL}/qa/answers/${answerId}/like`);
    return response.data;
  }
}; 